package com.fs.console.yuezi.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.pojo.CheckDetail;
import com.fs.common.pojo.CheckNote;
import com.fs.common.utils.StringTools;
import com.fs.common.yzMapper.CheckDetailMapper;
import com.fs.common.yzMapper.CheckNoteMapper;
import com.fs.console.base.enums.CodeEnum;
import com.fs.console.base.service.BaseService;
import com.fs.console.base.utils.ApplicationUtils;
import com.fs.console.base.vo.Criteria;
import com.fs.console.base.vo.DataGrid;
import com.fs.console.base.vo.JsonResponse;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author DeveloperKK
 * @desc 问卷管理
 */
@Slf4j
@Controller
@RequestMapping("/console/checkNote/rewrite")
public class CheckNoteController {
    @Autowired
    private BaseService baseService;
    @Autowired
    private CheckNoteMapper checkNoteMapper;
    @Autowired
    private CheckDetailMapper checkDetailMapper;
    @Getter
    @Setter
    @Value("${layui.upload.path}")
    private String layuiUploadPath;

    @ResponseBody
    @RequestMapping("/getDisease")
    @ApiOperation(value = "获取问卷信息")
    //@PassToken
    public JsonResponse getDisease(HttpServletRequest request){
        try {
            Map<String, Object> data = ApplicationUtils.getRequestDatas();
            QueryWrapper<Object> wrapper = new Criteria<Object>().addAuto(data);
            wrapper.eq("status",0);
            wrapper.orderByAsc("sort");
            List<Object> list = baseService.findAllWithSort("com.fs.common.pojo.Disease", request, wrapper);
            DataGrid grid = DataGrid.treeBuilder(list,"id","parentId");
            return JsonResponse.success(grid.getRows());
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    

    @ResponseBody
    @RequestMapping("/newAdd")
    public JsonResponse newAdd(HttpServletRequest request,String json) {
        try {
            String userId = ApplicationUtils.getUserId();
            JSONObject jsonObject = JSON.parseObject(json);
            CheckNote checkNote = JSON.parseObject(jsonObject.get("checkNote").toString(), CheckNote.class);
            checkNote.setCreaterId(userId);
            if(StringUtils.isNotBlank(checkNote.getAvoidFood())){
                List<String> strings = JSON.parseArray(checkNote.getAvoidFood(), String.class);
                checkNote.setAvoidFood(StringTools.listToString(strings,"#"));
            }
            CheckDetail face = JSON.parseObject(jsonObject.get("face").toString(), CheckDetail.class);
            CheckDetail tongue = JSON.parseObject(jsonObject.get("tongue").toString(), CheckDetail.class);
            CheckDetail questionnaire = JSON.parseObject(jsonObject.get("questionnaire").toString(), CheckDetail.class);
            if(StringUtils.isNotBlank(questionnaire.getCheckWords())){
                List<String> strings = JSON.parseArray(questionnaire.getCheckWords(), String.class);
                questionnaire.setCheckWords(StringTools.listToString(strings,"#"));
            }
            checkNoteMapper.insert(checkNote);
            checkDetailMapper.insert(face);
            checkDetailMapper.insert(tongue);
            checkDetailMapper.insert(questionnaire);
            return JsonResponse.success("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

}
